<?php

class Action
{

    public function cinema() {
        // 影院
        //        $path = ROOT_PATH . '/excel';
        //        $DATA_ZZB = file($path . '/zzb.csv');
        //        var_dump($DATA_ZZB);

        //var_dump(_model('user_store')->getList(array('type'=>100)));
        //        $us = _model('user_store')->getList(array('type' => 100));
        //        $us_index = array();
        //        foreach ($us as $item) {
        //            $us_index[$item['user_id']] = $item;
        //        }

        $s = _model('store')->getList(array('type' => 100));
        $s_index = array();
        $ags_index = array();
        $sign_index = array();
        foreach ($s as $item) {
            $s_index[$item['user_id']] = $item;
            $ags_index[] = $item['agency_id'];
            $sign_index[] = $item['sign_user_id'];
        }

        $u = _model('user')->getList(array('id' => array_merge(array_keys($s_index), $ags_index, $sign_index)));
        $u_index = array();
        foreach ($u as $item) {
            $u_index[$item['id']] = $item;
        }
        //
        //        $agu = _model('user')->getList(array('id' => $ags_index));
        //        $agu_index = array();
        //        foreach ($agu as $item) {
        //            $agu_index[$item['id']] = $item;
        //        }
        //
        //        $signu = _model('user')->getList(array('id' => $sign_index));
        //        $signu_index = array();
        //        foreach ($signu as $item) {
        //            $signu_index[$item['id']] = $item;
        //        }
        //var_dump($u_index);


        $fixed = array();
        foreach ($s_index as $user_id => $item) {
            if ($item['is_enable'] == 1 && $item['is_delete'] == 1) {
                $fixed[] = join(',', array(
                    'id' => $item['id'],
                    'user_id' => $item['user_id'],
                    'title' => $item['title'],
                    'realname' => $u_index[$user_id]['realname'],
                    'mobile' => $u_index[$user_id]['mobile'],
                    'address' => $item['address'],
                    'agency_id' => $item['agency_id'],
                    'agency_name' => $u_index[$item['agency_id']]['realname'],
                    'agency_mobile' => $u_index[$item['agency_id']]['mobile'],
                    'sign_user_id' => $item['sign_user_id'],
                    'sign_user_name' => empty($u_index[$item['sign_user_id']]) ? ''
                        : $u_index[$item['sign_user_id']]['realname'],
                    'sign_user_mobile' => empty($u_index[$item['sign_user_id']]) ? ''
                        : $u_index[$item['sign_user_id']]['mobile'],
                    'add_time' => $item['add_time'],
                    'is_auto' => $u_index[$user_id]['is_auto'],
                ));
            }
        }

        array_unshift($fixed, join(",", array(
            '影院ID(门店)',
            '用户ID',
            '影院名称',
            '影院负责人',
            '影院负责人手机号',
            '影院地址',
            '合伙人ID',
            '合伙人姓名',
            '合伙人手机号',
            '市场专员ID',
            '市场专员姓名',
            '市场专员手机号',
            '创建时间',
            '是否自动生成'
        )));

        array_unshift($fixed, join(",", array(
            'store.id',
            'store.user_id',
            'store.title',
            'user.realname',
            'user.mobile',
            'store.address',
            'store.agency_id',
            'user.mobile(agency_name)',
            'user.mobile(agency_mobile)',
            'store.sign_user_id',
            'user.id(sign_user_name)',
            'user.mobile(sign_user_mobile)',
            'store.add_time',
            'user.is_auto',
        )));

        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . '影院信息.csv');
        header("Content-Transfer-Encoding: binary");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        echo join("\n", $fixed);
    }

}

?>